package org.springblade.flow.drawingapproval.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.minio.MinioClient;
import io.minio.errors.*;
import jodd.util.StringUtil;
import lombok.SneakyThrows;
import org.apache.poi.ss.usermodel.Workbook;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.support.Kv;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.StringPool;
import org.springblade.flow.business.service.IFlowService;
import org.springblade.flow.core.constant.ProcessConstant;
import org.springblade.flow.core.entity.BladeFlow;
import org.springblade.flow.core.utils.FlowUtil;
import org.springblade.flow.drawingapproval.constants.DrawingApprovalConstants;
import org.springblade.flow.drawingapproval.entity.DrawingApproval;
import org.springblade.flow.drawingapproval.mapper.DrawingApprovalMapper;
import org.springblade.flow.drawingapproval.service.IDrawingApprovalService;
import org.springblade.flow.drawingapproval.vo.DrawingApprovalVO;
import org.springblade.mach.entity.DrawingNumberApplicationDetail;
import org.springblade.mach.entity.DrawingUpload;
import org.springblade.mach.service.IDrawingNumberApplicationDetailService;
import org.springblade.mach.service.IDrawingUploadService;
import org.springblade.mach.utils.ExcelUtils;
import org.springblade.mach.utils.MachUtils;
import org.springblade.modules.resource.builder.oss.OssBuilder;
import org.springblade.modules.resource.entity.Oss;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

import static liquibase.pro.packaged.kt.e;

@Service
public class DrawingApprovalServiceImpl extends BaseServiceImpl<DrawingApprovalMapper, DrawingApproval> implements IDrawingApprovalService {

	@Autowired
	private IFlowService flowService;
	@Autowired
	private IUserService userService;
	@Autowired
	private IDrawingUploadService drawingUploadService;
	@Autowired
	private IDrawingNumberApplicationDetailService drawingNumberApplicationDetailService;
	@Autowired
	private OssBuilder ossBuilder;
	@Autowired
	private ExcelUtils excelUtils;
	@Autowired
	private MachUtils machUtils;

	@Override
	@SneakyThrows
	@Transactional(rollbackFor = Exception.class)
	public Boolean startDrawingApproval(DrawingApproval drawingApproval) {
		// 设置状态为已开启
		drawingApproval.setStatus(DrawingApprovalConstants.START_STATUS);

		if (Func.isEmpty(drawingApproval.getId())) {
			// 保存图纸审批信息
			drawingApproval.setApplyTime(DateUtil.now());
			this.save(drawingApproval);
			drawingUploadService.updateDrawingUploadStatus(drawingApproval.getDrawingUploadId(),DrawingUpload.STATUS_APPROVAL);
			// 图纸名称
			String drawingApprovalTable = FlowUtil.getBusinessTable(ProcessConstant.DRAWING_APPROVAL_KEY);

			// 启动流程
			Kv variables = Kv.create()
				.set(ProcessConstant.TASK_VARIABLE_CREATE_USER, AuthUtil.getUserName());
			//.set("taskUser", TaskUtil.getTaskUser());

			BladeFlow flow = flowService.startProcessInstanceById(drawingApproval.getProcessDefinitionId(),
				FlowUtil.getBusinessKey(drawingApprovalTable, String.valueOf(drawingApproval.getId())), variables);
			if (Func.isNotEmpty(flow)) {
				drawingApproval.setProcessInstanceId(flow.getProcessInstanceId());
				this.updateById(drawingApproval);
			} else {
				throw new ServiceException("开启流程失败!");
			}

		} else {
			this.updateById(drawingApproval);
		}

		return true;
	}

	@Override
	public DrawingApprovalVO getDrawingApprovalDetail(Long id) {
		if (id == null) {
			throw new ServiceException("参数不能为空!");
		}
		DrawingApproval drawingApproval = this.getById(id);
		if (drawingApproval != null) {
			DrawingApprovalVO drawingApprovalVO = new DrawingApprovalVO();
			BeanUtil.copy(drawingApproval, drawingApprovalVO);
			if (StringUtil.isNotEmpty(drawingApproval.getTaskUser())) {
				User taskUser = userService.getById(drawingApproval.getTaskUser());
				if (taskUser != null) {
					drawingApprovalVO.setTaskUserName(taskUser.getAccount());
				}
				User applicantUser = userService.getById(drawingApproval.getCreateUser());
				if (applicantUser != null) {
					drawingApprovalVO.setApplicantName(applicantUser.getAccount());
				}
			}
			if (drawingApproval.getDrawingUploadId() != null) {
				DrawingUpload drawingUpload = drawingUploadService.getById(drawingApproval.getDrawingUploadId());
				if (drawingUpload != null) {
					drawingApprovalVO.setDrawingNumber(drawingUpload.getDrawingNumber());
					drawingApprovalVO.setDrawingName(drawingUpload.getDrawingName());
				}
			}
			return drawingApprovalVO;
		}
		return null;
	}

	@Override
	public DrawingApproval getDrawingApprovalByProcessInstanceId(String processInstanceId) {
		if (StringUtil.isEmpty(processInstanceId)) {
			throw new ServiceException("参数不能为空!");
		}
		return baseMapper.getDrawingApprovalByProcessInstanceId(processInstanceId);
	}
}
